package com.xxh.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxh.blog.dao.EssayDao;
import com.xxh.blog.entity.vo.EssayDetailVo;
import com.xxh.blog.entity.vo.EssayVo;
import com.xxh.blog.model.EssayDocument;
import com.xxh.blog.mq.PostMqMessage;
import com.xxh.blog.repository.EssayRespository;
import com.xxh.blog.service.SearchService;
import com.xxh.blog.service.UserService;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    EssayRespository essayRespository;

    @Resource
    EssayDao essayDao;

    @Autowired
    UserService userService;

    @Override
    public IPage search(Page page, String keyword) {

        // jpa 分页从 0 开始
        Long current = page.getCurrent()-1;
        Long size = page.getSize();
        //拿到分页信息，mybatics plus 的page 转为 jpa 的page
        Pageable pageable = PageRequest.of(current.intValue(), size.intValue());
        //构建搜索条件
        MultiMatchQueryBuilder builder = QueryBuilders.multiMatchQuery(keyword, "title", "authorName", "categoryName");
        org.springframework.data.domain.Page<EssayDocument> documents = essayRespository.search(builder, pageable);
        //jpa 的page 转为 mybatics plus 的page
        IPage pageData=new Page(page.getCurrent(),page.getSize(),documents.getTotalElements());
        pageData.setRecords(documents.getContent());
        return pageData;
    }


    /**
     * 数据初始化到es ,返回已经初始化的数据数量
     * @param records
     * @return
     */
    @Override
    public int initEsDate(List<EssayVo> records) {
        if(records==null||records.isEmpty()){
            return 0;
        }
        List<EssayDocument>documents=new ArrayList<>();
        records.stream().forEach(essayVo->{
            EssayDocument document = new EssayDocument();
            BeanUtils.copyProperties(essayVo,document);
            documents.add(document);
        });
        essayRespository.saveAll(documents);
        return documents.size();
    }

    /**
     * 添加或更新文章的时候更新索引
     * @param message
     */
    @Override
    public void creatOrUpdateIndex(PostMqMessage message) {
        Long postId = message.getPostId();

        QueryWrapper<EssayDetailVo> wrapper = new QueryWrapper<>();
        wrapper.eq("essay.id",postId);
        EssayDetailVo essayDetail = essayDao.getEssayDetail(null, wrapper);
        EssayDocument document = new EssayDocument();
        BeanUtils.copyProperties(essayDetail,document);
        essayRespository.save(document);
    }

    /**
     * 删除文章的时候删除索引
     * @param message
     */
    @Override
    public void removeIndex(PostMqMessage message) {

        essayRespository.deleteById(message.getPostId());
    }
}
